import { BaseLayer, IBaseLayerParams, RenderOptions } from './BaseLayer';
import { TextBuilder } from './TextLayer';
import logger from 'loglevel';

export class IconTextLayer extends BaseLayer {
  iconOptions: RenderOptions = {};
  private textBuilder?: TextBuilder;
  constructor(
    cfg: IBaseLayerParams,
    iconOptions?: RenderOptions,
    tb?: TextBuilder,
  ) {
    super(cfg);
    this.iconOptions = iconOptions || {};
    this.textBuilder = tb;
  }

  addToMap(): string {
    let textOptions = {};
    if (this.textBuilder) {
      textOptions = {
        'text-font': ['Open Sans Regular'],
        'text-field': this.textBuilder.format(), // 使用 TextBuilder 格式化文本
        'text-offset': [
          'interpolate',
          ['linear'],
          ['zoom'],
          0,
          [0, 0],
          10,
          [0, 0],
          22,
          [0.2, 0.2],
        ], // 文本偏移
        'text-anchor': 'top', // 文本锚点
      };
    }
    const options = {
      id: this.layerId,
      type: 'symbol',
      source: this.sourceId,
      ...this.iconOptions,
    };
    if (options.layout) {
      options.layout = {
        ...options.layout,
        ...textOptions, // 合并文本选项
      };
    }
    logger.info('IconTextLayer options:', options);
    this.map.addLayer(options as mapboxgl.SymbolLayerSpecification);
    return this.layerId;
  }
}
